package com.gxuwz.attend.dao;

import com.gxuwz.attend.entity.Classes;
import com.gxuwz.attend.entity.Course;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.ObjIntConsumer;

public class CourseDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 添加课程数据
     * @param course
     */
    public void insertCourse(Course course){
        String sql = "insert into sys_course (courseID,classID,courseName,year,term,hour)values(?,?,?,?,?,?)";
        jdbcTemplate.update(sql,course.getCourseID(),course.getClassID(),course.getCourseName(),course.getYear(),course.getTerm(),course.getHour());
    }

    /**
     * 删除课程数据
     * @param courseID
     */
    public void deleteCourse(String courseID){
        String sql = "delete from sys_course where courseID=?";
        jdbcTemplate.update(sql,courseID);
    }

    /**
     * 更新课程数据
     * @param course
     */
    public void updateCourse(Course course){
        String sql = "update sys_course set classID=?,courseName=?,year=?,term=?,hour=? where courseID=?";
        jdbcTemplate.update(sql,course.getClassID(),course.getCourseName(),course.getYear(),course.getTerm(),course.getHour(),course.getCourseID());
    }

    /**
     * 查询全部课程数据
     * @return
     */
    public List<Course> queryCourses(){
        String sql = "select * from sys_course";
        List<Object> paramValues = new ArrayList<>();
        List<Course> courseList = new ArrayList<>();
        courseList = jdbcTemplate.query(sql, paramValues.toArray(), new RowMapper<Course>() {
            @Override
            public Course mapRow(ResultSet resultSet, int i) throws SQLException {
                Course course1 = new Course();
                String courseID = resultSet.getString("courseID");
                String classID = resultSet.getString("classID");
                String courseName = resultSet.getString("courseName");
                String year = resultSet.getString("year");
                String term = resultSet.getString("term");
                Integer hour =  resultSet.getInt("hour");
                course1.setCourseID(courseID);
                course1.setClassID(classID);
                course1.setCourseName(courseName);
                course1.setYear(year);
                course1.setTerm(term);
                course1.setHour(hour);
                return course1;
            }
        });
        return courseList;
    }

    /**
     * 模糊查询课程数据
     * @param course
     * @return
     */
    public List<Course> queryCourse(Course course){
        String sql = "select * from sys_course where 1=1";
        List<Object> paramValues = new ArrayList<>();
        List<Course> courseList = new ArrayList<>();
        if (course!=null){
            if (course.getCourseID()!=null&&!"".equals(course.getCourseID().trim())){
                sql+=" and courseID=?";
                paramValues.add(course.getCourseID());
            }
            if (course.getClassID()!=null&&!"".equals(course.getClassID().trim())){
                sql+=" and classID=?";
                paramValues.add(course.getClassID());
            }
            if (course.getYear()!=null&&!"".equals(course.getYear().trim())){
                sql+=" and year=?";
                paramValues.add(course.getYear());
            }
            if (course.getTerm()!=null&&!"".equals(course.getTerm().trim())){
                sql+=" and term=?";
                paramValues.add(course.getTerm());
            }
            if (course.getHour()!=null&&!"".equals(course.getHour())){
                sql+=" and hour=?";
                paramValues.add(course.getHour());
            }
            if (course.getCourseName()!=null&&!"".equals(course.getCourseName().trim())){
                sql+=" and courseName like ?";
                paramValues.add(course.getCourseName()+"%");
            }
        }
        courseList = jdbcTemplate.query(sql, paramValues.toArray(), new RowMapper<Course>() {
            @Override
            public Course mapRow(ResultSet resultSet, int i) throws SQLException {
                Course course1 = new Course();
                String courseID = resultSet.getString("courseID");
                String classID = resultSet.getString("classID");
                String courseName = resultSet.getString("courseName");
                String year = resultSet.getString("year");
                String term = resultSet.getString("term");
                Integer hour =  resultSet.getInt("hour");
                course1.setCourseID(courseID);
                course1.setClassID(classID);
                course1.setCourseName(courseName);
                course1.setYear(year);
                course1.setTerm(term);
                course1.setHour(hour);
                return course1;
            }
        });
        return courseList;
    }
}
